package com.eight.util;



import com.eight.pojo.SysMenu;
import com.eight.vo.MenuVo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Random;

/**
 * @Author: zyp
 * @CreateTime: 2022-12-19  22:24
 * @Version: 1.0
 */
public class MenuLeveUtil {
    public static List<SysMenu> MakeLayersMenu(List<SysMenu> menus) {
        // 先选出一级菜单
        List<SysMenu> fatherMenu = menus.stream()
                .filter(sysMenu -> sysMenu.getParentId().equals(0)).collect(Collectors.toList());
        // 二级菜单
        for (SysMenu menuOne : fatherMenu) {
            ArrayList<SysMenu> menuTwo = new ArrayList<>();
            for (SysMenu menu : menus) {
                if (menu.getParentId().equals(menuOne.getId())){
                    menuTwo.add(menu);
                }
            }
            menuOne.setChildMenu(menuTwo);
        }
        return fatherMenu;
    }
    // 转换前端锁需要的数据
    public static List<MenuVo> MakeMenuVo(List<SysMenu> menus) {
        List<MenuVo> m1 = new ArrayList<>();
        for (SysMenu menu : menus) {
            MenuVo menuVo = new MenuVo();
            menuVo.setId(menu.getId());
            menuVo.setPid(menu.getParentId());
            menuVo.setIndex(menu.getUrl());
            menuVo.setTitle(menu.getMenuName());
            menuVo.setIcon(menu.getIcon());
            m1.add(menuVo);
        }
        return m1;
    }

    public static List<MenuVo> MakeLayersMenuVo(List<MenuVo> menus) {
        // 先选出一级菜单
        List<MenuVo> fatherMenu = menus.stream()
                .filter(sysMenu -> sysMenu.getPid().equals(0)).collect(Collectors.toList());
        // 二级菜单
        for (MenuVo menuOne : fatherMenu) {
            ArrayList<MenuVo> menuTwo = new ArrayList<>();
            for (MenuVo menu : menus) {
                if (menu.getPid().equals(menuOne.getId())){
                    menuTwo.add(menu);
                }
            }
            menuOne.setSubs(menuTwo);
        }
        return fatherMenu;
    }
    // 随机字符串生成
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

}
